package com.rui.study.micro.userself.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @program: study
 * @description:
 * @author: Yaowr
 * @create: 2018-11-20 14:31
 **/
public class MySqlLock {

    private Connection connection;

    public MySqlLock(Connection connection) {
        this.connection = connection;
    }

    public boolean lock(String method) {
        try {
            connection.setAutoCommit(false);
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery("SELECT * FROM t_sync_method WHERE method_name = '" + method + "' FOR UPDATE");
            if (rs != null) {

            }
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public void unlock() {
        try {
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
